﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyBlog.Model;
using MyBlog.Common;
using MyBlog.ServiceInterface;
using System.IO;
using Webdiyer.WebControls.Mvc;
using System.ServiceModel.Syndication;
using System.Xml;
using System.Text;

namespace MyBlog.Web.Controllers
{
    public class HomeController : BaseController
    {
        //[Dependency]
        //public IAccountService _s { set; get; }
        [TrackRequest]
        public ActionResult Index()
        {
            var tags = WebCache.GetTags().OrderByDescending(x => x.ArticleModels.Count).Take(30).ToList();
            ViewBag.tags = tags;
            var allArticles = WebCache.GetArticles();
            ViewBag.hotClickArticles = allArticles.Where(x => x.AllowSendIndex && x.PassToIndex).OrderByDescending(x => x.ReadCount).Take(6).ToList();
            ViewBag.newArticles = allArticles.Where(x => x.AllowSendIndex).OrderByDescending(x => x.CreateTime).Take(6).ToList();
            ViewBag.recomArticles = allArticles.Where(x => x.AllowSendIndex && x.PassToIndex)
                .OrderByDescending(x => x.ArticleCommentModels.Count).ThenByDescending(x => x.ReadCount).Take(6).ToList();
            ViewBag.hotUsers = WebCache.UsersCache.OrderByDescending(x => x.ConcernMeUsers.Count).Take(10).ToList();
            ViewBag.regUsers = WebCache.UsersCache.OrderByDescending(x => x.RegTime).Take(15).ToList();
            return View();
        }

        [HttpPost]
        public PartialViewResult _latelyArticles(int page = 1, int pageSize = 10)
        {
            var latelyArticles = WebCache.GetArticles().Where(x => x.AllowSendIndex && x.PassToIndex).OrderByDescending(x => x.CreateTime);
            var total = latelyArticles.Count();
            ViewBag.totalPage = total % pageSize == 0 ? total / pageSize : (total / pageSize + 1);
            var pageArticles = latelyArticles.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return PartialView(pageArticles);
        }

        public PartialViewResult _BlogHead()
        {
            ViewBag.user = UserSession.LoginUserInfo;
            return PartialView();
        }

        public void Rss()
        {
            var latelyArticles = WebCache.GetArticles().Where(x => x.AllowSendIndex).OrderByDescending(x => x.CreateTime).Take(10);
            List<SyndicationItem> items = new List<SyndicationItem>();
            foreach (var item in latelyArticles)
            {
                SyndicationItem article = new SyndicationItem();
                article.Title = new TextSyndicationContent(item.Title);
                article.Content = SyndicationContent.CreatePlaintextContent(item.Content);
                article.Summary = SyndicationContent.CreatePlaintextContent(item.Content.StripHTML().Sub(100));
                article.PublishDate = item.CreateTime;
                items.Add(article);
            }
            SyndicationFeed feed = new SyndicationFeed("小博客rss订阅", "小博客首页文章订阅", new Uri("http://www.xiaoboke.net/home/rss"), items);
            StringBuilder sb = new StringBuilder();
            using (var xmlWriter = XmlWriter.Create(sb))
            {
                feed.SaveAsRss20(xmlWriter);
                xmlWriter.Close();
            }
            Response.ContentType = "application/xml";
            Response.Write(sb);
        }

    }
}
